/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.UserDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.SanPham;
import pojo.User;

/**
 *
 * @author OSIRIS
 */
@WebServlet(name = "Signin", urlPatterns = {"/signin.do"})
public class Signin extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            String action = request.getParameter("action");
            HttpSession session = request.getSession();
            String view = "index.jsp";
            if (action != null) {


                if (action.equals("view")) {

                    request.setAttribute("page", "Signin");
                }
                if (action.equals("login")) {
                    //check account

                    if (session.getAttribute("current_user") == null) {

                        String mail = request.getParameter("txtEmail");
                        String pass = request.getParameter("txtPass");
                        //UserDAO  infoDao=new UserDAO();
                        List<User> ds = UserDAO.Login(mail, pass);
                        view = "message.jsp";

                        if (ds != null) {
                            User info = ds.get(0);
                            session.setAttribute("current_user", info);
                            // request.setAttribute("message", "Xin Chào "+info.getHoTen());
                            response.sendRedirect("index.do");
                            return;
                        } else {
                            //view="message.jsp";
                            request.setAttribute("message", "Mật khẩu hoặc mail sai");
                        }

                    }
                }
                if (action.equals("logout")) {
                    //delete all session
                    session.invalidate();
                    request.setAttribute("message", "Logout Success");
                }
            }

            RequestDispatcher rd = request.getRequestDispatcher(view);
            rd.forward(request, response);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
